/*
 * Author:	D. Curtiss
 * Date:	Spring 2011
 * Class:	CSE 510
 */
package global;

import java.io.*;
import java.lang.*;

// Other imports?

public class TID
{
	// global.TID extends Java.lang.Object;

	public int numRIDs;
	public int position;
	public RID[] recordIDs;

        //default blank Constructor.
	public TID () { }

        // Constructor.
	public TID(int numRIDs)
	{
		this.numRIDs = numRIDs;
		position = 0; // Arbitrary?
		recordIDs = new RID[numRIDs]; // Initialize it empty.
		for(int i = 0;i<numRIDs;i++)
			recordIDs[i] = new RID();
	}

	// Constructor.
	public TID(int numRIDs, int position)
	{
		this.numRIDs = numRIDs;
		this.position = position;
		recordIDs = new RID[numRIDs]; // Initialize it empty.
	}

	// Constructor.
	public TID(int numRIDs, int position, RID[] recordIDs)
	{
		this.numRIDs = numRIDs;
		this.position = position;

		// Is this the right way to instantiate the array of RIDs?
		for (int rid = 0; rid < this.numRIDs; rid++)
			this.recordIDs[rid] = recordIDs[rid];
	}

	// Make a copy of the given ID.
	public void copy(TID tid)
	{
		numRIDs = tid.numRIDs;
		position = tid.position;

		for (int rid = 0; rid < numRIDs; rid++)
			this.recordIDs[rid] = tid.recordIDs[rid];
	}

	// Compares two TID objects.
	public boolean equals(TID tid)
	{
		// Check if the integer values are equal.
		if ((this.numRIDs == tid.numRIDs) && (this.position == tid.position))
		{
			// If both numRIDs and position are equal, check each
			// item of the RID[] arrays.
			// If any entry is NOT equal, return false.
			for (int rid = 0; rid < this.numRIDs; rid++)
				if (this.recordIDs[rid] != tid.recordIDs[rid])
					return false;

			// All entries must be equal (otherwise it would have
			// returned false), so return true.
			return true;
		}

		// One of the initial conditions must have failed.
		else return false;
	}

	// Write the TID into a byte array at offset.
	public void writeToByteArray(byte[] array, int offset) throws java.io.IOException
	{
		Convert.setIntValue(numRIDs, offset, array);
		Convert.setIntValue(position, offset+4, array); // +4 to count for size of int?

		// How to write the recordIDs to an array? Iterate?
	}

	// Set the position attribute with the given value.
	public void setPosition(int position)
	{
		this.position = position;
	}

	// Set the RID of the given column.
	public void setRID(int column, RID recordID)
	{
		recordIDs[column] = recordID;
	}

        // Set the RIDs attribute with the given value.
	public void setRIDs(RID [] recordIDs)
	{
		this.recordIDs = recordIDs;
	}

        // Set the numRID attribute with the given value.
	public void setNumRID(int numRID)
	{
		this.numRIDs = numRID;
	}

};
